package com.power.services.tiCase;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.power.dao.tiCase.TiCaseDAO;
import com.power.model.tiCase.TICase;

@Service("tiCaseService")
public class TiCaseServiceImpl implements TiCaseService {
	@Autowired
	private TiCaseDAO tiCaseDAO;

	public void create(TICase tiCase) {
		tiCaseDAO.saveOrUpdate(tiCase);
	}

	public void delete(TICase tiCase) {
		tiCaseDAO.delete(tiCase);

	}

	public TICase read(Integer id) {
		return tiCaseDAO.load(id);
	}

	public void update(TICase tiCase) {
		this.tiCaseDAO.update(tiCase);

	}

	@SuppressWarnings("unchecked")
	public Map<String, Object> findTsCase(Map<String, String> paramMap) {
		Map<String, Object> restulMap = new HashMap<String, Object>();
		int start = Integer.parseInt(paramMap.get("start"));
		int limit = Integer.parseInt(paramMap.get("limit"));
		String str = " select  ts   from TICase ts where  1=1 ";
		if (!"-1".equals(paramMap.get("dpartId"))) {
			str += " and ts.departId =" + paramMap.get("dpartId");
		}
		if (!"-1".equals(paramMap.get("idcard"))) {
			str += " and ts.authorId ='" + paramMap.get("idcard") + "'";
		}
		if (!"-1".equals(paramMap.get("startDate"))
				&& !"-1".equals(paramMap.get("endDate"))) {
			String stareDate = paramMap.get("startDate");
			String endDate = paramMap.get("endDate");
			str += " and ( ts.createTime between '" + stareDate + " 00:00:00' and '"
					+ endDate + " 23:59:59' ) ";
		}
		List<TICase> data = tiCaseDAO.find(str, start, limit);
		List<TICase> countData = tiCaseDAO.find(str);
		int count = countData == null ? 0 : countData.size();
		restulMap.put("data", data);
		restulMap.put("count", count);
		return restulMap;
	}

	public void deleteCase(String deleteIds) {
		String[] ids = deleteIds.split("_");
		for (int i = 0; i < ids.length; i++) {
			tiCaseDAO.executeSql(" delete from  t_i_case where id =" + ids[i]);
		}
	}

	public void updateCaseRead(String id, String flag) {
		String str = "  update  t_i_case t set t.isRead=" + flag + " where id="
				+ id;
		tiCaseDAO.executeSql(str);
	}

	@SuppressWarnings("unchecked")
	public List<Object> findCaseList(Map<String, String> paramMap) {
		String str = " SELECT  ts.id,ts.create_time ,td.name NAME  FROM t_i_case ts,t_dd_data td WHERE  ts.depart_id = td.code AND td.DD_ID=2 and " +
				" ts.author_id ='"+paramMap.get("idCard")+"' order by create_time desc";
		 return tiCaseDAO.findBySQL(str);
	}
}
